package com.ygqh.baby.controller.admin;

import com.fasterxml.jackson.core.type.TypeReference;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.controller.utils.ExcelUtil;
import com.ygqh.baby.model.FortuneBagDetailVo;
import com.ygqh.baby.model.FortuneBagVo;
import com.ygqh.baby.po.YgFortuneBag;
import com.ygqh.baby.service.YgFortuneBagService;
import com.ygqh.baby.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: sunshuo
 * @date: 2019/1/17 14:30
 */
@Controller
@RequestMapping("admin/fortuneBag")
public class FortuneBagController extends BaseController<YgFortuneBag, Long> {

	@Autowired
	private YgFortuneBagService ygFortuneBagService;

	/**
	 * 福袋管理查询
	 *
	 * @param queryInfo   分页
	 * @param productName 福袋商品名称
	 * @return
	 */
	@RequestMapping("search")
	@ResponseBody
	public Map<String, Object> search(QueryInfo queryInfo, String productName) {
		ResultSet<FortuneBagVo> resultSet = ygFortuneBagService.search(queryInfo, productName);
		return resultSet.toDatatables(null);
	}

	/**
	 * 福袋产品明细查询
	 *
	 * @param queryInfo 分页
	 * @param skuCode   福袋SKU编码
	 * @return
	 */
	@RequestMapping("searchDetail")
	@ResponseBody
	public Map<String, Object> searchDetail(QueryInfo queryInfo, String batchNo, String skuCode) {
		ResultSet<FortuneBagDetailVo> resultSet = ygFortuneBagService.searchDetail(queryInfo, batchNo, skuCode);
		return resultSet.toDatatables(null);
	}

	/**
	 * 福袋信息保存
	 *
	 * @param fortuneBags 福袋信息
	 * @return
	 */
	@RequestMapping("saveBatch")
	@ResponseBody
	public Message saveBatch(String fortuneBags) {
		List<YgFortuneBag> list = JsonUtils.toObject(fortuneBags, new TypeReference<List<YgFortuneBag>>() {
		});
		return ygFortuneBagService.saveBatch(list, getSession().getTrueName());
	}

	/**
	 * 导入excel
	 *
	 * @param uploadFile 文件
	 * @return
	 */
	@RequestMapping("loadExcel")
	@ResponseBody
	public Message loadExcel(@RequestParam("uploadFile") MultipartFile uploadFile) {
		List<Map<Integer, Object>> excelInfo;
		try {
			excelInfo = ExcelUtil.readExcel(uploadFile);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return Message.error("读取导入数据错误", null);
		}
		return Message.success(ygFortuneBagService.resolveDataFromExcel(excelInfo));
	}

	@RequestMapping("findFortuneBagInfoByCopy")
	@ResponseBody
	public Message findFortuneBagInfoByCopy(@RequestParam String skuCode) {
		return Message.success(ygFortuneBagService.findFortuneBagInfoByCopy(skuCode));
	}

	@RequestMapping("findFortuneBagDetailByCopy")
	@ResponseBody
	public Message findFortuneBagDetailByCopy(@RequestParam String batchNo, @RequestParam String skuCode, String sizeName) {
		return Message.success(ygFortuneBagService.findFortuneBagDetailByCopy(batchNo, skuCode, sizeName));
	}

}
